﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace UserControls
{
    public partial class AdresaUC : StudioUserControl
    {

        private Int64 _idPartener = -1;
        public Int64 IdPartener
        {
            get { return _idPartener; }
            set {
                if (_idPartener != value)
                {
                    _idPartener = value;
                    SqlConnection con = StudioForm.DeschideConexiune(parentForm.workConnString);
                    this.adreseTableAdapter.Connection = con;
                    if (_idPartener == -1)
                        this.adreseTableAdapter.Fill(this.eSRPDataSet.Adrese);
                    else
                    {
                        this.adreseTableAdapter.FillByIDPartener(this.eSRPDataSet.Adrese, _idPartener);
                        eSRPDataSet.Adrese.IDPartenerColumn.DefaultValue = _idPartener;
                    }
                    StudioForm.InchideConexiune(con);
                    if (adreseBindingSource.Count == 0)
                        adreseBindingSource.AddNew(); 
                }
            }
        }

        public void InitDB(StudioForm sf)
        {
            if (parentForm == null)
            {
                parentForm = sf;
            }
            if (parentForm == null)
                return;
            SqlConnection con = StudioForm.DeschideConexiune(parentForm.workConnString);
            this.tariTableAdapter.Connection = con;
            this.tariTableAdapter.Fill(this.eSRPDataSet.Tari);
            this.tipAdresaTableAdapter.Connection = con;
            this.tipAdresaTableAdapter.Fill(this.eSRPDataSet.TipAdresa);
            this.judeteTableAdapter.Connection = con;
            this.judeteTableAdapter.Fill(this.eSRPDataSet.Judete);
            this.localitatiTableAdapter.Connection = con;
            this.localitatiTableAdapter.Fill(this.eSRPDataSet.Localitati);
            StudioForm.InchideConexiune(con);
        
        }

        public AdresaUC(): base()
        {
            InitializeComponent();
        }
   
        private void AdresaUC_Load(object sender, EventArgs e)
        {
            if (parentForm == null)
                return;
            parentForm.listBndSourceMain.Add(adreseBindingSource);
            parentForm.listTableAdaptor.Add(adreseTableAdapter.Adapter);
        }

        private void AdresaUC_Validating(object sender, CancelEventArgs e)
        {
            Boolean eEroare = false;
            if (stradaTB.Text.Trim() == String.Empty)
            {
                //MessageBox.Show("Va rugam completati numarul documentului!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                errorProvider1.SetError(stradaTB, "Va rugam completati strada adresei!");
                eEroare = true;
            }
            else
                errorProvider1.SetError(stradaTB, String.Empty);
            if (iDTipAdresaCB.SelectedIndex == -1)
            {
                errorProvider1.SetError(iDTipAdresaCB, "Va rugam completati tipul de adresa!");
                eEroare = true;            
            }
            else
                errorProvider1.SetError(iDTipAdresaCB, String.Empty);
            

            if(eEroare)
                return;
        }

        private void iDTipAdresaCB_SelectedValueChanged(object sender, EventArgs e)
        {
            if (iDTipAdresaCB.SelectedIndex != -1)
                errorProvider1.SetError(iDTipAdresaCB, String.Empty);
        }

        private void stradaTB_Enter(object sender, EventArgs e)
        {
            errorProvider1.SetError(stradaTB, String.Empty);
        }

        private void iDLocalitatiCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iDLocalitatiCB.SelectedItem == null)
                return;
            ESRPDataSet.LocalitatiRow lrow = ((DataRowView)iDLocalitatiCB.SelectedItem).Row as ESRPDataSet.LocalitatiRow;
            iDJudetComboBox.SelectedValue = lrow.IDJudet;
         }

        private void AdresaUC_Enter(object sender, EventArgs e)
        {
            //MessageBox.Show("Am intrat");
        }

        private void AdresaUC_VisibleChanged(object sender, EventArgs e)
        {
            if (parentForm == null)
                return;
            if (Visible)
            {
                parentForm.SetCurrentDataSource(adreseBindingSource);
            }
        }

        
    }
}
